package com.zhiwang.xiaoqu.widget.pageindicator

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import com.zhiwang.xiaoqu.R
import dp

class PageIndicatorView : View {


    var indicatorRadius: Int = 0

    @ColorInt
    var indicatorSelectColor: Int = 0

    @ColorInt
    var indicatorUnselectColor: Int = 0

    var paint = Paint()

    var wormWidth = 0f


    // indicator count
    var count: Int = 3
        set(value) {
            field = value
            requestLayout()
            invalidate()
        }


    var position: Int = 0
        set(value) {
            if (field != value) {
                field = value
                startAnimator()
            }
        }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {


        val typeArray = context.obtainStyledAttributes(attrs, R.styleable.PageIndicatorView)

        indicatorRadius =
            typeArray.getDimensionPixelSize(R.styleable.PageIndicatorView_indicator_radius, 9.dp)

        indicatorSelectColor = typeArray.getColor(
            R.styleable.PageIndicatorView_indicator_select_color,
            ContextCompat.getColor(context, R.color.colorPrimary)
        )

        indicatorUnselectColor = typeArray.getColor(
            R.styleable.PageIndicatorView_indicator_unselect_color,
            ContextCompat.getColor(context, R.color.colorAccent)
        )

        typeArray.recycle()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        //worm 宽度为三个小球直径
        val width = count * (indicatorRadius * 2 * 3)
        setMeasuredDimension(width, indicatorRadius * 2)
    }


    private fun startAnimator() {

        val valueAnimator = ValueAnimator.ofFloat(indicatorRadius * 2f, indicatorRadius * 6f)
            .setDuration(180)
        valueAnimator
            .addUpdateListener {
                wormWidth = it.animatedValue as Float
                invalidate()
            }

        valueAnimator.start()
    }


    //原先的小点没有动画效果，只是选中的位置的圆角矩形是从中心变长的
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        for (i in 0 until count) {
            val cx = 3f * indicatorRadius + i * 6f * indicatorRadius

            val cy = indicatorRadius.toFloat()

            paint.color = if (position != i) indicatorUnselectColor else indicatorSelectColor

            canvas?.drawCircle(cx, cy, indicatorRadius.toFloat(), paint)

            if (position == i) {
                canvas?.drawRoundRect(
                    cx - wormWidth / 2f,
                    0f,
                    cx + wormWidth / 2f,
                    indicatorRadius*2f,
                    indicatorRadius.toFloat(),
                    indicatorRadius.toFloat(),
                    paint
                )
            }
        }
    }
}